<?php declare (strict_types = 1);

namespace process\workbunny\rabbitmq;

use app\admin\model\Order;
use Bunny\Async\Client as BunnyClient;
use Bunny\Channel as BunnyChannel;
use Bunny\Message as BunnyMessage;
use Workbunny\WebmanRabbitMQ\Builders\QueueBuilder;
use Workbunny\WebmanRabbitMQ\Constants;

class OrderBuilderDelayed extends QueueBuilder
{
    /** @inheritdoc  */
    protected ?string $connection = 'rabbitmq';

    /**
     * @var array = [
     *   'name'           => 'example',
     *   'delayed'        => false,
     *   'prefetch_count' => 1,
     *   'prefetch_size'  => 0,
     *   'is_global'      => false,
     *   'routing_key'    => '',
     * ]
     */
    protected array $queueConfig = [
        // 队列名称 ，默认由类名自动生成
        'name'           => 'process.workbunny.rabbitmq.OrderBuilderDelayed',
        // 是否延迟
        'delayed'        => true,
        // QOS 数量
        'prefetch_count' => 0,
        // QOS size
        'prefetch_size'  => 0,
        // QOS 全局
        'is_global'      => false,
        // 路由键
        'routing_key'    => '',
    ];

    /** @var string 交换机类型 */
    protected string $exchangeType = Constants::DIRECT;

    /** @var string|null 交换机名称,默认由类名自动生成 */
    protected ?string $exchangeName = 'process.workbunny.rabbitmq.OrderBuilderDelayed';

    /** @inheritDoc */
    public function handler(BunnyMessage $message, BunnyChannel $channel, BunnyClient $client): string
    {
        // TODO 请重写消费逻辑
        $orderId = $message->content;
        // 查询订单
        $order = Order::find($orderId);
        if ($order->status == 1) {
            $order->status = 4;
            $order->save();
        }
        return Constants::ACK;
    }
}
